import time

from selenium.webdriver.support.ui import WebDriverWait,Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

class Base():
    # 初始化
    def __init__(self,driver):
        self.driver = driver
    # 网址
    def open_url(self,url):
        self.driver.get(url)
    # 时间戳
    def get_time(self):
        em = time.strftime('%Y%m%d_%H%M%S',time.localtime())
        self.driver.save_screenshot('./Png'+'Img'+em+'.png')

    # 定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,1).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            self.get_time()

    # 输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 点击
    def click(self,loc):
        self.find_element(loc).click()
    # 输入出文本
    def text(self,loc):
        return self.find_element(loc).text

    # 滚动条（执行js代码）
    def scrollTo(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

    # 弹框处理
    def accept(self):
        alert=self.driver.switch_to.alert
        alert.accept()
    def dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    # iframe处理
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    def frame_uncon(self):
        self.driver.switch_to.default_content()

    # 切换页面
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 鼠标事件
    def a_double(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()

    # 键盘事件
    def key_enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)

    # 浏览器事件
    def refresh(self):
        self.driver.refresh()

    # 下拉框选择
    def select(self,loc,x):
        e=Select(self.find_element(loc))
        e.select_by_value(x)